/**
 * 
 */
package com.et114.modules.commons;

import com.et114.core.control.IConverter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.text.ParseException;

/*
 * @author guanhw
 */
public class DateConverter implements IConverter {
	/**
	 * 日期格式化对象.
	 */
	private  SimpleDateFormat	df			= new SimpleDateFormat ( );
	
	/**
	 * 模式集合.
	 */
	private static Set < String >	patterns	= new HashSet < String > ( );
	// 注册一下日期的转换格式
	static {
		DateConverter.patterns.add ( "yyyy-MM-dd" );
		DateConverter.patterns.add ( "yyyy-MM-dd HH:mm" );
		DateConverter.patterns.add ( "yyyy-MM-dd HH:mm:ss" );
		DateConverter.patterns.add ( "yyyy/MM/dd HH:mm:ss" );
		
		//
		DateConverter.patterns.add ( "MM/dd/yyyy HH:mm" );
		DateConverter.patterns.add ( "MM/dd/yyyy HH:mm:ss" );
//		DateConverter.patterns.add ( "MM/dd/yyyy" );
	}
	
	/**
	 * 日期转换器.
	 * 
	 * @param type
	 *            Class
	 * @param value
	 *            Object return Date Object.
	 */
	@ SuppressWarnings ( "unchecked" )
	public Object convert ( Class type , Object value ) {
	   if ( value == null ) {
			return null;
		} else if ( value instanceof String ) {
			Object dateObj = null;
			if ( ( ( String ) value ).length ( ) <= 10 && ( ( String ) value ).indexOf ( "/" ) >= 0  ) {
				df	= new SimpleDateFormat ( "MM/dd/yyyy" );
				try {
					dateObj = df.parse ( ( String ) value );
					return dateObj ;
				}
				catch ( ParseException ex ) { 
					
				}
			}
			
			if ( ( ( String ) value ).length ( ) <= 16 && ( ( String ) value ).indexOf ( "/" ) >= 0  ) {
				df	= new SimpleDateFormat ( "MM/dd/yyyy HH:mm" );
				try {
					dateObj = df.parse ( ( String ) value );
					return dateObj ;
				}
				catch ( ParseException ex ) { 
					
				}
			}
			
			
			if ( ( ( String ) value ).length ( ) <= 10 && ( ( String ) value ).indexOf ( "-" ) >= 0  ) {
				df	= new SimpleDateFormat ( "yyyy-MM-dd" );
				try {
					dateObj = df.parse ( ( String ) value );
					return dateObj ;
				}
				catch ( ParseException ex ) { 
					
				}
			}
			
			if ( ( ( String ) value ).length ( ) <= 16 && ( ( String ) value ).indexOf ( "/" ) >= 0  ) {
				df	= new SimpleDateFormat ( "yyyy-MM-dd HH:mm" );
				try {
					dateObj = df.parse ( ( String ) value );
					return dateObj ;
				}
				catch ( ParseException ex ) { 
					
				}
			}
			
			/*
			Iterator it = patterns.iterator ( );
			while (it.hasNext ( )) {
				try {
					String pattern = ( String ) it.next ( );
					//df.applyPattern ( pattern );
					df			= new SimpleDateFormat ( pattern );
					dateObj = df.parse ( ( String ) value );
					break;
				} catch ( ParseException ex ) {
					//ex.printStackTrace ( ) ;
					// do iterator continue
				}
			} */
			return dateObj;
			
		
		}
		else {
			return null;
		}
		
		
//		if ( value == null ) {
//			return value;
//		}
//		if ( value instanceof Date ) {
//			return value;
//		}
//		if ( value instanceof String ) {
//			try {
//				return format.parse ( ( String ) value );
//			} catch ( ParseException e ) {
//				e.printStackTrace ( );
//			}
//		}
//		return null;

	}
	
	@ SuppressWarnings ( "unchecked" )
	public Class getDefaultClass ( ) {
		// TODO Auto-generated method stub
		return java.util.Date.class;
	}
	
	
	public static void main( String[] s ) throws Exception {
		SimpleDateFormat df	= new SimpleDateFormat ( "MM/dd/yyyy HH:mm" );
		String d = df.format (  new Date() ) ; 

		System.out.println ( d.toString ( ) ) ;
	}
}
